class EditorHistory {


    private list: AbstractCommand[] = [];

    private currentIndex = -1;

    add(command: AbstractCommand) {
        this.list.push(command);
        command.execute();
        this.currentIndex += 1;
    }

    redo(index: number) {
        const length = this.list.length;
        for (let i = this.currentIndex; i < index; i++) {
            //王泽：
            //重做操作是把【下一个】命令进行重做，所以是当前游标+1
            const command = this.list[i + 1];
            command.execute();
        }
        this.currentIndex = index;
    }

    revert(index: number) {

        const length = this.list.length;
        for (let i = this.currentIndex; i > index; i--) {
            const command = this.list[i];
            command.revert();
        }
        this.currentIndex = index;

    }

    revertOnce() {
        this.revert(this.currentIndex - 1);
    }

    redoOnce() {
        this.redo(this.currentIndex + 1);
    }

}


export abstract class AbstractCommand {


    abstract execute(): void;


    abstract revert(): void;
}

export class TestCommand extends AbstractCommand {


    constructor(private from: number, private to: number) {
        super();
    }

    execute(): void {
        console.log(`从${this.from}到${this.to}`)
    }
    revert(): void {
        console.log(`从${this.to}到${this.from}`);
    }
}


export const editorHistory = new EditorHistory();